const {findFlagList, bestFindClose} = require("./Find");
const {backHome} = require("./RoleOut");
//盾牌兵运行逻辑
module.exports.shield = creep => {
    let roomVisual = new RoomVisual(creep.room.name);
    let HealBody = creep.getActiveBodyparts(HEAL);
    if (HealBody > 0) {
        roomVisual.circle(creep.pos, {radius: 5, fill: 'transparent', stroke: 'grey', strokeWidth: 0.1});
        //治疗最近血量最少的creep
        let nearbyCreeps = creep.room.find(FIND_MY_CREEPS, {
            filter: (nearbyCreep) => {
                return nearbyCreep.hits < nearbyCreep.hitsMax;
            }
        })
        let nearbyHit = creep.pos.findInRange(nearbyCreeps, 5)
        if (nearbyHit.length > 0) {
            creep.say("🥤");
            nearbyHit.sort((a, b) => a.hits - b.hits)
            creep.bestHeal(nearbyHit[0]);
        }
    }
    let sign = Memory.attackSign;
    if (sign === "move" || sign === "auto") {
        let defensePos = findFlagList("attack_defense", 1);
        if (defensePos) {
            if (!creep.pos.inRangeTo(defensePos, 2)) {
                creep.say("🚙")
                creep.bestMove(defensePos);
            }
        }
        return
    }
    if (sign === "back") {
        let spawn = Game.spawns["BlackJun"];
        if (spawn) {
            creep.say("🔙")
            creep.bestMove(spawn);
        }
        return;
    }
    if (sign === "renew") {
        let spawn = creep.room.find(FIND_MY_SPAWNS)[0];
        if (creep.ticksToLive < 1400) {
            creep.say("🥩")
            if (spawn.renewCreep(creep) === ERR_NOT_IN_RANGE) {
                creep.bestMove(spawn);
            }
        }
        return;
    }
    if (sign === "recycle") {
        let spawn = creep.room.find(FIND_MY_SPAWNS)[0];
        if (spawn.recycleCreep(creep) === ERR_NOT_IN_RANGE) {
            creep.bestMove(spawn);
        }
    }
}
//战士运行逻辑
module.exports.attack = creep => {
    let sign = Memory.attackSign;
    if (sign === "back") {
        let spawn = Game.spawns["BlackJun"];
        if (spawn) {
            creep.say("🔙")
            creep.bestMove(spawn);
        }
        return;
    }
    //3格范围内存在敌兵,直接先攻击
    let npcCreep = creep.pos.findInRange(creep.room.find(FIND_HOSTILE_CREEPS,{filter:hostileCreep=>{
            if (hostileCreep.getActiveBodyparts(ATTACK)+hostileCreep.getActiveBodyparts(RANGED_ATTACK)>0){
                return true;
            }
        }}), 2)[0];
    if (npcCreep) {
        creep.say("💢");
        if (creep.bestAttack(npcCreep) === ERR_INVALID_TARGET) {
            creep.memory.closeId = undefined;
        }
        return;
    }
    if (sign === "move") {
        let defensePos = findFlagList("attack", 1);
        if (defensePos) {
            if (!creep.pos.inRangeTo(defensePos, 2)) {
                creep.say("🚙")
                creep.bestMove(defensePos);
            }
        }
        return
    }
    if (sign === "auto") {
        let defensePos = findFlagList("attack", 1);
        if(defensePos &&creep.room.name!==defensePos.pos.roomName){
            creep.say("🚙")
            creep.bestMove(defensePos);
            return
        }
        let towerList = creep.room.find(FIND_HOSTILE_STRUCTURES, {filter: site => site.structureType === STRUCTURE_TOWER || site.structureType ===STRUCTURE_INVADER_CORE});
        if (towerList.length > 0) {
            creep.say("💢");
            creep.bestAttack(bestFindClose(creep, towerList));
            return;
        }
        let allHostile = creep.room.find(FIND_HOSTILE_CREEPS,{filter:hostileCreep=>{
                if (hostileCreep.getActiveBodyparts(ATTACK)+hostileCreep.getActiveBodyparts(RANGED_ATTACK)>0){
                    return true;
                }
            }});
        if (allHostile.length > 0) {
            let closeHostile = creep.findBindClose(allHostile);
            if (closeHostile) {
                creep.say("💢");
                creep.bestMove(closeHostile)
            }else{
                creep.memory.closeId=undefined;
            }
            return;
        }
        let hostileSpawnList = creep.room.find(FIND_HOSTILE_SPAWNS);
        if (hostileSpawnList.length > 0) {
            creep.say("💢");
            creep.bestAttack(hostileSpawnList[0]);
            return;
        }
        let allCreeps = creep.room.find(FIND_HOSTILE_CREEPS);
        if(allCreeps.length>0){
            creep.say("💢");
            creep.bestAttack(allCreeps[0]);
            return;
        }
        let allExtension=creep.room.find(FIND_HOSTILE_STRUCTURES,{
            filter:site=>{
                return site.structureType===STRUCTURE_EXTENSION
            }
        })
        if(allExtension.length>0){
            creep.say("💢");
            creep.bestAttack(allExtension[0]);
            return;
        }
        if (defensePos) {
            if (!creep.pos.inRangeTo(defensePos, 2)) {
                creep.say("🚙")
                creep.bestMove(defensePos);
            }else{
                defensePos.remove();
            }
        }
    }
    if (sign === "renew") {
        let spawn = creep.room.find(FIND_MY_SPAWNS)[0];
        if (creep.ticksToLive < 1400) {
            creep.say("🥩")
            if (spawn.renewCreep(creep) === ERR_NOT_IN_RANGE) {
                creep.bestMove(spawn);
            }
        }
        return;
    }
    if (sign === "recycle") {
        let spawn = creep.room.find(FIND_MY_SPAWNS)[0];
        if (spawn.recycleCreep(creep) === ERR_NOT_IN_RANGE) {
            creep.bestMove(spawn);
        }
    }
}
//拆除兵运行逻辑
module.exports.dismantle = creep => {
    let sign = Memory.attackSign;
    if (sign === "move") {
        let dismantle=findFlagList("dismantle",1);
        if(dismantle.pos.roomName!==creep.room.name){
            creep.say("🚙")
            creep.bestMove(dismantle);
        }else{
            let defensePos = bestFindClose(creep,findFlagList("dismantle"));
            if (defensePos) {
                if (!creep.pos.inRangeTo(defensePos, 2)) {
                    creep.say("🚙")
                    creep.bestMove(defensePos);
                } else {
                    creep.say("🏗️");
                    let site = defensePos.pos.lookFor(LOOK_STRUCTURES)[0];
                    if (!site || site.hits === 0) {
                        // 删除旗帜
                        defensePos.remove();
                    }
                    creep.bestDismantle(site);
                }
            }
            return;
        }
    }
    if (sign === "auto") {

    }
}
//土匪运行逻辑
module.exports.bandits = creep => {
    creep.initFull();
    if (creep.memory.isFull) {
        backHome(creep);
        return;
    }
    let defensePos = findFlagList("attack", 1);
    if (creep.room.name !== defensePos.pos.roomName) {
        creep.say("🚙")
        creep.bestMove(defensePos);
        return;
    }
    let droppedResourceList = creep.room.find(FIND_DROPPED_RESOURCES).filter(source => source.amount > 20);
    if (droppedResourceList.length > 0) {
        creep.say("📦")
        creep.bestPickup(creep.findBindClose(droppedResourceList));
        return;
    }
    let deadCreepList = creep.room.find(FIND_TOMBSTONES);
    if (deadCreepList.length > 0) {
        let haveOther;
        for (let storeKey in deadCreepList[0].store) {
            haveOther = storeKey;
            break;
        }
        if (haveOther){
            creep.say("⚰")
            creep.bestWithdraw(creep.findBindClose(deadCreepList), haveOther)
            return;
        }
    }
    let storeList = creep.room.find(FIND_STRUCTURES, {
        filter: site => {
            return (site.structureType === STRUCTURE_CONTAINER || site.structureType === STRUCTURE_STORAGE) && site.usedStore() > 0
        }
    })
    if (storeList.length > 0) {
        let closeStore = creep.findBindClose(storeList);
        let haveOther=RESOURCE_ENERGY;
        if(closeStore){
            for (let storeKey in closeStore.store) {
                haveOther = storeKey;
                break;
            }
        }
        creep.say("🥫");
        creep.bestWithdraw(closeStore, haveOther);
        return;
    }
    let teamCreepList = creep.room.find(FIND_MY_CREEPS);
    if (teamCreepList.length === 0) {
        creep.memory.isFull = true;
    }
}